import { login, getProfile, getUserDetail } from '@/api/user'
// import { now } from 'core-js/core/date'
const state = {
  // token: ''
  userInfo: {},
  token: localStorage.getItem('token') || ''
}
// 修改
const mutations = {
  setToken(state, token) {
    state.token = token

    localStorage.setItem('token', token) || ''
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  removeToken(state) {
    state.token = ''
    localStorage.removeItem('token')
  },
  removeUserInfo(state) {
    state.userInfo = {}
  }

}
// 存放异步代码
const actions = {
  async login(store, loginForm) {
    const res = await login(loginForm)
    store.commit('setToken', res)
    console.log('action正在被调用')
    localStorage.setItem('timestamp', Date.now())
  },
  async getUserInfo(store) {
    const res = await getProfile()
    // store.commit('setUserInfo', res)
    const detail = await getUserDetail(res.userId)
    console.log(detail)
    store.commit('setUserInfo', {
      ...res,
      ...detail,
      staffPhoto: 'http://baidu.com/hahahaheihei.jpg'
    })
  },
  logout(store) {
    store.commit('removeToken')
    store.commit('removeUserInfo')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

